package servlet;

import bean.User;
import db.DbFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = null;
        try {
            user = DbFactory.getUserDao().login(username, password);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new IOException(e);
        }
        if (user != null) {
            request.getSession().setAttribute("currentUser", user);
            response.sendRedirect("./user/front.do");
        } else {
            response.sendRedirect("./failed.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
